package model;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Film {
	private int fid, uid,cid,nid;
	private String film_nvn, film_nen,film_time, quality, diractor,production_date, intro;
	private int level, view;
	private Date post_on;
	public Film(){}; 
	public Film(int fid, int uid, int cid, int nid, String film_nvn,
			String film_nen, String film_time, String quality, String diractor,
			String production_date, String intro, int level, int view,
			Date post_on) {
		this.fid = fid;
		this.uid = uid;
		this.cid = cid;
		this.nid = nid;
		this.film_nvn = film_nvn;
		this.film_nen = film_nen;
		this.film_time = film_time;
		this.quality = quality;
		this.diractor = diractor;
		this.production_date = production_date;
		this.intro = intro;
		this.level = level;
		this.view = view;
		this.post_on = post_on;
	}
	public int getFid() {
		return fid;
	}
	public void setFid(int fid) {
		this.fid = fid;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public int getNid() {
		return nid;
	}
	public void setNid(int nid) {
		this.nid = nid;
	}
	public String getFilm_nvn() {
		return film_nvn;
	}
	public void setFilm_nvn(String film_nvn) {
		this.film_nvn = film_nvn;
	}
	public String getFilm_nen() {
		return film_nen;
	}
	public void setFilm_nen(String film_nen) {
		this.film_nen = film_nen;
	}
	public String getFilm_time() {
		return film_time;
	}
	public void setFilm_time(String film_time) {
		this.film_time = film_time;
	}
	public String getQuality() {
		return quality;
	}
	public void setQuality(String quality) {
		this.quality = quality;
	}
	public String getDiractor() {
		return diractor;
	}
	public void setDiractor(String diractor) {
		this.diractor = diractor;
	}
	public String getProduction_date() {
		return production_date;
	}
	public void setProduction_date(String production_date) {
		this.production_date = production_date;
	}
	public String getIntro() {
		return intro;
	}
	public void setIntro(String intro) {
		this.intro = intro;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public int getView() {
		return view;
	}
	public void setView(int view) {
		this.view = view;
	}
	public Date getPost_on() {
		return post_on;
	}
	public void setPost_on(Date post_on) {
		this.post_on = post_on;
	}

	public int getSizeDB(){
		int result = 0;
		PreparedStatement sqlu = null;
		Connection con = new ConnectDB().getConnection();
		try {
			sqlu = con
					.prepareStatement("select COUNT(fid) from film");
			ResultSet rs = sqlu.executeQuery();
			while (rs.next()) {
				return rs.getInt(1);
			}
			sqlu.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public String checkName() {
		String nv = this.film_nvn;
		String ne = this.film_nen;
			if((nv == null)){
				if(ne == null){ return "Bạn chưa nhập tên phim."; }
				else{
					ne.trim();
					if(ne.equals("")){
						return "Bạn chưa nhập tên phim.";
					}else{
						PreparedStatement sqlu = null;
						Connection con = new ConnectDB().getConnection();
						try {
							sqlu = con
									.prepareStatement("select fid from film where film_nen = ?");
							sqlu.setString(1, ne);
							ResultSet rs = sqlu.executeQuery();
							while (rs.next()) {
								return "Tên phim đã tồn tại.";
							}
							sqlu.close();
							con.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
			}else 
				if(nv.trim().equals("")){
					if(ne == null){ return "Bạn chưa nhập tên phim.";}
					else{
						ne.trim();
						if(ne.equals("")){
							return "Bạn chưa nhập tên phim.";
						}else{
							PreparedStatement sqlu = null;
							Connection con = new ConnectDB().getConnection();
							try {
								sqlu = con
										.prepareStatement("select fid from film where film_nen = ?");
								sqlu.setString(1, ne);
								ResultSet rs = sqlu.executeQuery();
								while (rs.next()) {
									return "Tên phim đã tồn tại.";
								}
								sqlu.close();
								con.close();
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}
				}else{
					nv.trim();
					PreparedStatement sqlu = null;
					Connection con = new ConnectDB().getConnection();
					try {
						sqlu = con
								.prepareStatement("select fid from film where film_nvn = ?");
						sqlu.setString(1, nv);
						ResultSet rs = sqlu.executeQuery();
						while (rs.next()) {
							return "Tên phim đã tồn tại.";
						}
						sqlu.close();
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
					if(ne!= null){
						try {
							sqlu = con
									.prepareStatement("select fid from film where film_nen = ?");
							sqlu.setString(1, ne);
							ResultSet rs = sqlu.executeQuery();
							while (rs.next()) {
								return "Tên phim đã tồn tại.";
							}
							sqlu.close();
							con.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
			
		return "";
	}
	public String checkNewName() {
		String nv = this.film_nvn;
		String ne = this.film_nen;
			if((nv == null)){
				if(ne == null){ return "Bạn chưa nhập tên phim."; }
				else{
					ne.trim();
					if(ne.equals("")){
						return "Bạn chưa nhập tên phim.";
					}else{
						PreparedStatement sqlu = null;
						Connection con = new ConnectDB().getConnection();
						try {
							sqlu = con
									.prepareStatement("select fid from film where film_nen = ?");
							sqlu.setString(1, ne);
							ResultSet rs = sqlu.executeQuery();
							while (rs.next()) {
								if(rs.getInt(1)!= this.fid)
								return "Tên phim đã tồn tại.";
							}
							sqlu.close();
							con.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
			}else 
				if(nv.trim().equals("")){
					if(ne == null){ return "Bạn chưa nhập tên phim.";}
					else{
						ne.trim();
						if(ne.equals("")){
							return "Bạn chưa nhập tên phim.";
						}else{
							PreparedStatement sqlu = null;
							Connection con = new ConnectDB().getConnection();
							try {
								sqlu = con
										.prepareStatement("select fid from film where film_nen = ?");
								sqlu.setString(1, ne);
								ResultSet rs = sqlu.executeQuery();
								while (rs.next()) {
									if(rs.getInt(1)!= this.fid)
									return "Tên phim đã tồn tại.";
								}
								sqlu.close();
								con.close();
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}
				}else{
					nv.trim();
					PreparedStatement sqlu = null;
					Connection con = new ConnectDB().getConnection();
					try {
						sqlu = con
								.prepareStatement("select fid from film where film_nvn = ?");
						sqlu.setString(1, nv);
						ResultSet rs = sqlu.executeQuery();
						while (rs.next()) {
							if(rs.getInt(1)!= this.fid)
							return "Tên phim đã tồn tại.";
						}
						sqlu.close();
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
					if(ne!= null){
						try {
							sqlu = con
									.prepareStatement("select fid from film where film_nen = ?");
							sqlu.setString(1, ne);
							ResultSet rs = sqlu.executeQuery();
							while (rs.next()) {
							if(rs.getInt(1)!= this.fid)
								return "Tên phim đã tồn tại.";
							}
							sqlu.close();
							con.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
			
		return "";
	}
	public boolean Create() {
		PreparedStatement sqlr = null;
		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("INSERT INTO film( uid, cid, nid, film_nvn, film_nen, film_time, quality, diractor, production_date, intro, level, view, post_on) VALUES (? , ? , ? , ? , ? , ? ,  ? , ? , ? , ? , ? , ? , NOW())");
			sqlr.setInt(1, this.uid);
			sqlr.setInt(2,this.cid );
			sqlr.setInt(3, this.nid);
			sqlr.setString(4,this.film_nvn );
			sqlr.setString(5, this.film_nen);
			sqlr.setString(6,this.film_time );
			sqlr.setString(7,this.quality );
			sqlr.setString(8,this.diractor );
			sqlr.setString(9,this.production_date );
			sqlr.setString(10,this.intro );
			sqlr.setInt(11,this.level  );
			sqlr.setInt(12,this.view  );
			int number = sqlr.executeUpdate();
			if (number > 0) {
				return true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean upDate(int fid) {
		fid = (fid == 0 )? this.fid: fid; 
		PreparedStatement sqlr = null;
		boolean result = false;

		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("UPDATE film SET   uid = ?, cid = ?, nid = ?, film_nvn = ?, film_nen = ?, film_time = ?, quality = ?, diractor =?, production_date = ? , intro = ? , level = ? , view = ?  WHERE fid = ? LIMIT 1");
			sqlr.setInt(1, this.uid);
			sqlr.setInt(2,this.cid );
			sqlr.setInt(3, this.nid);
			sqlr.setString(4,this.film_nvn );
			sqlr.setString(5, this.film_nen);
			sqlr.setString(6,this.film_time );
			sqlr.setString(7,this.quality );
			sqlr.setString(8,this.diractor );
			sqlr.setString(9,this.production_date );
			sqlr.setString(10,this.intro );
			sqlr.setInt(11,this.level  );
			sqlr.setInt(12,this.view  );
			sqlr.setInt(13, fid);
			int number = sqlr.executeUpdate();
			if (number > 0) {
				result = true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public Film getFilm(int id){
		Film film = null;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT * FROM film WHERE fid = ?");
			sqlp.setInt(1, id);
			ResultSet rs = sqlp.executeQuery();
			while(rs.next() == true){
				int fid = rs.getInt(1);
				int uid = rs.getInt(2);
				int cid = rs.getInt(3);
				int nid = rs.getInt(4);
				String film_nvn = rs.getString(5);
				String film_nen = rs.getString(6);
				String film_time = rs.getString(7);
				String quality = rs.getString(8);
				String diractor = rs.getString(9);
				String production_date = rs.getString(10);
				String intro = rs.getString(11);
				int level = rs.getInt(12);
				int view = rs.getInt(13);
				Date post_on = rs.getDate(14);
				film = new Film(fid, uid, cid, nid, film_nvn, film_nen, film_time, quality, diractor, production_date, intro, level, view, post_on);
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return film;
	}
	public Film getFilmByName(String name) {
		String n = null;
		name = (name == null) ? this.getFilm_nen() : name;
		n = (name == null) ? this.getFilm_nvn() : name;
		PreparedStatement sqll = null;
		Film result = null;
		Connection c = new ConnectDB().getConnection();
		try {
			sqll = c.prepareStatement("select * from film where film_nvn = ? OR film_nen = ? ");
			sqll.setString(1, name);
			sqll.setString(2, n);
			ResultSet rs = sqll.executeQuery();
			while (rs.next() == true) {
				int fid = rs.getInt(1);
				int uid = rs.getInt(2);
				int cid = rs.getInt(3);
				int nid = rs.getInt(4);
				String film_nvn = rs.getString(5);
				String film_nen = rs.getString(6);
				String film_time = rs.getString(7);
				String quality = rs.getString(8);
				String diractor = rs.getString(9);
				String production_date = rs.getString(10);
				String intro = rs.getString(11);
				int level = rs.getInt(12);
				int view = rs.getInt(13);
				Date post_on = rs.getDate(14);
				result = new Film(fid, uid, cid, nid, film_nvn, film_nen, film_time, quality, diractor, production_date, intro, level, view, post_on);
			
			}
			sqll.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return result;
	}
	public int[] getFID(String orby /*n_name */){
		if(orby==null){
			orby="film_nen";
		}else{
			orby=orby.trim();
			if (!((orby.equals("fid"))||(orby.equals("uid"))||(orby.equals("cid"))
					||(orby.equals("nid"))
					||(orby.equals("film_nen"))
					||(orby.equals("film_nvn"))
					||(orby.equals("film_time"))
					||(orby.equals("quality"))
					||(orby.equals("diractor"))
					||(orby.equals("production_date"))
					||(orby.equals("intro"))
					||(orby.equals("level"))
					||(orby.equals("view"))
					||(orby.equals("post_on"))
					)) {
				orby="film_nen";
			}
		}
		int[] result = new int[this.getSizeDB()];
		int i =0;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT fid FROM film ORDER BY "+ orby + " ASC  ");
			ResultSet rs = sqlp.executeQuery();
			while(rs.next() == true){
				result[i] = rs.getInt(1);
				i++;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public String toString (){
		return this.fid + " " + 
		this.uid + " " + 
		this.cid+ " " + 
		this.nid + " " + 
		this.film_nvn + " " + 
		this.film_nen + " " + 
		this.film_time+ " " + 
		this.quality + " " + 
		this.diractor + " " + 
		this.production_date + " " + 
		this.intro + " " + 
		this.level + " " + 
		this.view+ " " + 
		this.post_on  ;
	}
	public boolean isAcID(String fid) throws IOException{ /*kiem tra tinh hop le cua nid va co nam trong DB hay khong*/
		if(fid==null) return false;
		if(fid.trim().equals(""))return false;
		fid.trim();
		int id = -1;
		try {
			id = Integer.parseInt(fid);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT fid FROM film where fid= ? ");
			sqlp.setInt(1, id);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				return true;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean del(int fid){
		PreparedStatement delpd = null;
		Connection con = new ConnectDB().getConnection();
		try {
			delpd = con.prepareStatement("DELETE FROM film WHERE fid = ?  ");
			delpd.setInt(1, fid);
			int number = delpd.executeUpdate();
			if (number > 0) {
				return true;
			}
			delpd.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  false;
	}
}
